#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-08-22 01:14:23
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$


from web.handler.api import BaseApiHandler
from lib.tornado_routes import route
from tornado import gen
from tornado.web import asynchronous
from lib import libimage,libpage
from web.models.api.dorm.like import Like

head_title = '点赞'

@route(r'api/dorm/like/new/?',name='like_new')
class NewLikeHandler(BaseApiHandler):
    '''
    @title 点赞
    @method post
    @param studentId 姓名 默认：''
    @param empId 保洁职工id 默认：''
    @retfield isOk 点赞成功与否1成功0失败 
    @return {status:200, message:'', data:null}---status:500(系统错误)400(请传入必要参数)401(已经点过赞)
    @description 点赞
    '''
    @asynchronous
    @gen.coroutine
    def post(self):
        student_id = self.get_argument('studentId', default='')
        emp_id = self.get_argument('empId', default='')

        if student_id == '' or emp_id == '':
            self.write_fail(None ,400, '请传入必要参数')
        else:
            ret = yield Like().like(student_id,emp_id)
            if ret == '-1':
                self.write_fail(None ,500, '系统错误')
            elif ret == '0':
                self.write_success({'isOk':0})
            elif ret == '1':
                self.write_success({'isOk':1})
            elif ret == '2':
                self.write_fail(None, 401, '已经点过赞')
        self.finish()

@route(r'api/dorm/like/ranklist/?',name='like_list')
class RanklistHandler(BaseApiHandler):
    '''
    @title 点赞排行榜
    @method get
    @param timeType 时间类型1今天2近一周3本月 默认：1
    @param page 第几页 默认：1
    @param offset 每页显示多少数据 默认：20
    @retfield count 点赞数量 
    @retfield empName 保洁职工名 
    @retfield img 头像 
    @retfield age 年龄 
    @retfield dormName 宿管楼名 
    @return {status:200, message:'', data:null}---status:500(系统错误)400(请传入必要参数)
    @description 点赞排行榜
    '''
    @asynchronous
    @gen.coroutine
    def get(self):
        timeType = self.get_argument('timeType', default=1)
        page = self.get_argument('page', default=1)
        offset = self.get_argument('offset', default=20)

        ret = yield Like().ranklist(timeType, libpage.start(page, offset), offset)
        if ret == '-1':
            self.write_fail(None ,500, '系统错误')
        else:
            self.write_success(ret)
        self.finish()


@route(r'api/dorm/like/cancel/?',name='like_cancel')
class CancelLikeHandler(BaseApiHandler):
    '''
    @title 取消点赞
    @method post
    @param studentId 姓名 默认：''
    @param empId 保洁职工id 默认：''
    @retfield isOk 取消点赞成功与否1成功0失败 
    @return {status:200, message:'', data:null}---status:500(系统错误)400(请传入必要参数)
    @description 取消点赞
    '''
    @asynchronous
    @gen.coroutine
    def post(self):
        student_id = self.get_argument('studentId', default='')
        emp_id = self.get_argument('empId', default='')

        print student_id
        print emp_id

        if student_id == '' or emp_id == '':
            self.write_fail(None ,400, '请传入必要参数')
        else:
            ret = yield Like().cancel_like(student_id,emp_id)
            if ret == '-1':
                self.write_fail(None ,500, '系统错误')
            elif ret == '0':
                self.write_success({'isOk':0})
            elif ret == '1':
                self.write_success({'isOk':1})
        self.finish()
